package dao;

import entity.License;
import entity.User;
import tools.DBTools;

import java.sql.*;
import java.util.ArrayList;

public class Admin_UserDAO {
    Connection conn = null;//创建connection对象
    PreparedStatement ps = null;//初始化PreparedStatement对象
    ResultSet rs = null;//初始化resultSet对象

    public boolean admin_login(String userName, String userPwd) {
        boolean adminLogin = true;//管理员是否登录成功，默认不成功
        Connection conn = DBTools.getInstance().getConnection();
        try {
            String sql = "SELECT * FROM TBL_USER_ACCOUNT WHERE LOGIN_ID = ? AND PASSWORD = ? AND USER_TYPE = 'admin'";
            ps = conn.prepareStatement(sql);
            ps.setString(1, userName);
            ps.setString(2, userPwd);
            rs = ps.executeQuery();
            if (rs.next()) {
                adminLogin = true;
            } else {
                adminLogin = false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            adminLogin = false;
        } finally {
            DBTools.getInstance().closeConnection(conn, ps, rs);
        }
        return adminLogin;
    }

    /**
     * 登陆成功 显示所有商户信息
     *
     * @return
     */
    public ArrayList<User> show_shop() {
        ArrayList<User> listUser = new ArrayList<>();
        User tua;
        Connection conn = DBTools.getInstance().getConnection();
        try {
            String sql = "SELECT * FROM TBL_USER_ACCOUNT WHERE USER_TYPE = 'shop'";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            if (rs.next()) {
                tua = new User();
                tua.setId(rs.getInt("ID"));//获取用户ID
                tua.setLoginId(rs.getString("LOGIN_ID"));//获取登录账号
                tua.setNickname(rs.getString("NICK_NAME"));//获取昵称
                tua.setAddress(rs.getString("ADDRESS"));//获取地址
                tua.setIdentityCard(rs.getString("IDENTITY_CARD").substring(0, 10));//获取身份证号码(截取长度只显示十位)
                tua.setSex(rs.getString("SEX"));//获取性别
                tua.setRealname(rs.getString("REAL_NAME"));//获取真实姓名
                tua.setUserStatus(rs.getString("USER_STATUS"));
                listUser.add(tua);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
        	DBTools.getInstance().closeConnection(conn, ps, rs);
        }
        return listUser;
    }

    /**
     * 根据商户id  对某一个商户进行封禁或者解封处理
     *
     * @param shopId
     * @return
     */

    public int shop_operation(String shopId, String butVal) {
        String sql = "";
        if (butVal.equals("封禁商户")) {//根据获取的按钮类型 执行相应的操作
            sql = "UPDATE TBL_USER_ACCOUNT SET USER_STATUS = 'confine3d' WHERE LOGIN_ID = ?";
        } else {
            sql = "UPDATE TBL_USER_ACCOUNT SET USER_STATUS = 'common' WHERE LOGIN_ID = ?";
        }
        Connection conn = DBTools.getInstance().getConnection();//创建Connection 对象链接
        int rs = 0;//结果集 默认为失败 0   成功为1
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, shopId);
            rs = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBTools.getInstance().closeConnection(conn, ps, null);//关闭Connection  关闭PreparedStatement  没有ResultSet 给定null 不关闭
        }
        return rs;
    }

    /**
     * 展示所有申请成为商家的用户列表
     *
     * @param shopType
     * @return
     */
    public ArrayList<User> show_shopType(String shopType) {
        ArrayList<User> listUser = new ArrayList<>();
        User tua;
        Connection conn = DBTools.getInstance().getConnection();
        if (shopType.equals("user")) {//获取查询类型根据类型执行相应查询
            try {
                String sql = "SELECT * FROM TBL_USER_ACCOUNT WHERE ID = (SELECT id FROM tbl_license_detail)";
                ps = conn.prepareStatement(sql);
                rs = ps.executeQuery();
                if (rs.next()) {
                    tua = new User();
                    tua.setId(rs.getInt("ID"));//获取用户ID
                    tua.setLoginId(rs.getString("LOGIN_ID"));//获取登录账号
                    tua.setNickname(rs.getString("NICK_NAME"));//获取昵称
                    tua.setAddress(rs.getString("ADDRESS"));//获取地址
                    tua.setIdentityCard(rs.getString("IDENTITY_CARD").substring(0, 10));//获取身份证号码(截取长度只显示十位)
                    tua.setSex(rs.getString("SEX"));//获取性别
                    tua.setRealname(rs.getString("REAL_NAME"));//获取真实姓名
                    tua.setUserStatus(rs.getString("USER_STATUS"));
                    listUser.add(tua);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
               DBTools.getInstance().closeConnection(conn, ps, rs);
            }
        } else {
            return show_shop();
        }
        return listUser;
    }

    /**
     * 审核用户成为商家的申请
     *
     * @param shopId
     * @return
     */
    public License shop_apply(String shopId) {
        License license = new License();//创建用户申请类实体对象
        Connection conn = DBTools.getInstance().getConnection();
        try {
            String sql = "SELECT * FROM tbl_license_detail WHERE id = (SELECT ID FROM TBL_USER_ACCOUNT WHERE LOGIN_ID = ?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, shopId);
            rs = ps.executeQuery();
            if(rs.next()){
                license.setUserId(rs.getInt("id"));
                license.setLicenseId(rs.getInt("license_id"));
                license.setCardImg(rs.getString("card_img"));
                license.setEstablishTime(rs.getString("establish_time"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
        	DBTools.getInstance().closeConnection(conn, ps, rs);
        }
        return license;
    }

    /**
     * 审核用户申请
     * @param userId
     * @param yes_no
     * @return
     */
    public int apply_yesOrNo(int userId,String yes_no){
        String sql = "";
        int num = 0;//记录是否成功 默认不成功
        if(yes_no.equals("yes")){
            sql = "UPDATE TBL_USER_ACCOUNT SET USER_TYPE = 'shop' WHERE ID = ?";
        }else{
            sql = "DELETE FROM tbl_license_detail WHERE ID = ?";
        }
        System.out.println(sql);
        Connection conn = DBTools.getInstance().getConnection();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, userId);
            num = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
        	DBTools.getInstance().closeConnection(conn, ps, rs);
        }
        return num;
    }
}
